package test;
import java.security.MessageDigest;	
import java.security.NoSuchAlgorithmException;;
public class Encrypt {
	public String md5(String text) {
		return digest(text,"MD5");
	}
	public String sha1(String text) {
		return digest(text,"SHA1");
	}
	private static String digest(String text,String alg) {
		String mdvalue;
		try
		{
			MessageDigest md=MessageDigest.getInstance(alg);
			md.update(text.getBytes());
			byte result[]=md.digest();
			StringBuffer buf=new StringBuffer();
			String tmp;
			int intValue;
			for(int pos=0;pos<result.length;pos++)
			{
				intValue=result[pos]+256;
				intValue%=256;
				tmp=Integer.toHexString(intValue);
				if(tmp.length()==1) {
					buf.append("0");
				}
				buf.append(tmp);
			}
			mdvalue=buf.toString().toUpperCase();
		}catch(NoSuchAlgorithmException e) {
			mdvalue=text;
		}
		return mdvalue;
	}
	public static void main(String[] args) {
		Encrypt enc=new Encrypt();
		System.out.println("MD5:"+enc.md5("GOOD"));
		System.out.println("SHA1:"+enc.sha1("GOOD"));
	}
}
